//
//  HouseViewController.m
//  Product_NEWS
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 FCH. All rights reserved.
//

#import "HouseViewController.h"
#import "HouseDetailViewController.h"
#import "HouseViewCell.h"

@interface HouseViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,assign)BOOL isUpLoading;
@property(nonatomic,assign)NSInteger page;
@property(nonatomic,retain)HouseViewCell *entertainmentCell;
@end

@implementation HouseViewController
-(void)dealloc{
    [_myTableView release];
    [_modelArray release];
    [_entertainmentCell release];
   
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.modelArray = [NSMutableArray array];
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 * SCREEN_HEIGHT)];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    self.isUpLoading = NO;
    self.page = 1;
    [self addHeader];
    [self addFooter];

}

-(void)addHeader
{
    __block HouseViewController *vc = self;
    [self.myTableView addHeaderWithCallback:^{
        
        vc.page = 1;
        vc.isUpLoading = NO;
        [vc GetPersonList:vc.page];
        
        
    }];
    
    
    //自动刷新
    [self.myTableView headerBeginRefreshing];
}

-(void)addFooter
{
    __block HouseViewController *vc = self;
    [self.myTableView addFooterWithCallback:^{
        
        vc.page ++;
        vc.isUpLoading = YES;
        [vc GetPersonList:vc.page];
    }];
}



-(void)GetPersonList:(NSInteger)nextCursor{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    //****参数cursor的值: 第一页默认为0，下一页就是请求下来数据中next_cursor的值
    //****参数count 的值: 是指定一页显示多少条数据
    NSString *url_string = [NSString stringWithFormat:@"http://www.tingwen.me/akcms_category.php?id=9&page=%ld",self.page];
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        NSMutableArray *allArray = [NSMutableArray array];
        
        allArray  = responseObject;
        
        if (self.isUpLoading == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.modelArray removeAllObjects];
        }
        
        for (NSDictionary *dic  in allArray) {
            
            HouseModel *model = [[HouseModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.modelArray addObject:model];
          
        }
        
        
        [self.myTableView footerEndRefreshing];
        [self.myTableView headerEndRefreshing];
        [self.myTableView reloadData];
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.myTableView headerEndRefreshing];
        [self.myTableView footerEndRefreshing];
        MBProgressHUD *mb  = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                              mb.mode = MBProgressHUDModeText;
                              
        mb.labelText = @"网络不给力";
        [mb hide:YES afterDelay:0.5];
        
        
        
    }];
    
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.modelArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = nil;
    HouseViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (!cell) {
        cell = [[HouseViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
       
        
    }
    
    
    HouseModel *model = [self.modelArray objectAtIndex:indexPath.row];
    
    cell.model = model;
    
    return cell;
    
}


//cell点击 进入详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HouseDetailViewController *detail = [[HouseDetailViewController alloc]init];
    HouseModel *model = [[HouseModel alloc]init];
    model = [self.modelArray objectAtIndex:indexPath.row];
    
    detail.model = model;
    
    [self.navigationController pushViewController:detail animated:YES];
    
    
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * SCREEN_HEIGHT;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
